import lodashlodash from 'lodash'
import { PNodeType } from '~/models/graph/node/type'

export function randomName(prefix?: string, postfix?: string): string {
  return `${prefix?prefix:''}${Math.random().toString(36).slice(-8)}${postfix?postfix:''}`
}

export function uniqueNaming(name: string, exclude: string[]) {
  if (!exclude.includes(name)) {
    return name
  }
  else {
    for(let i=0;i<exclude.length + 1;i++) {
      const seq = i + 2
      const _name = `${name}_${seq}`
      if (!exclude.includes(_name)) {
        return _name
      }
    }
  }

  throw new Error(`Failed to name, default name: "${name}", exclude: ${exclude}`)
}

export const lodash = lodashlodash
export const remove = lodash.remove

export function getBgByType(type?: PNodeType) {
  // node.prototype.type === PNodeType.Process ? `var(--bg-process)` : `var(--bg-function)`
  if (type === PNodeType.Process) {
    return 'var(--bg-process)'
  }
  else if (type === PNodeType.Function) {
    return 'var(--bg-function)'
  }
  else if (type === PNodeType.PureFunc) {
    return 'var(--bg-pure)'
  }
  else if (type === PNodeType.Getter) {
    return 'var(--bg-getter)'
  }
  else if (type === PNodeType.Expression) {
    return 'var(--bg-pure)'
  }
  else if (type === PNodeType.Setter) {
    return 'var(--bg-setter)'
  }
  else {
    return 'var(--bg-builtin)'
  }
}

export function throwWhenMount(reason?: any) {
  console.error(reason);
  
  throw new Error(`throwWhenMount: ${reason ?? '错误的挂载'}`)
}